package cn.itcast.web.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;

public class PersonAction extends Action {

	@Resource PersonService personService;
	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//从application范围内得到spring容器实例
		/*WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
				this.getServlet().getServletContext());
		PersonService personService= (PersonService)ctx.getBean("personService");
		List<Person> persons = personService.getPersons();
		request.setAttribute("persons", persons);
		return mapping.findForward("list");*/
		request.setAttribute("persons", personService.getPersons());
		return mapping.findForward("list");
	}

}
